<?php

class Index {

  public static function main() {
    date_default_timezone_set("PRC");
    ini_set("display_errors","Off");
    spl_autoload_register(function ($className){
      $data = explode("\\",$className);
      if (empty($data[0] || $data[0] == "")){
        array_shift($data);
      }
      $last_item = array_pop($data);
      $map_data = array_map("strtolower",$data);
      $map_data[] = $last_item;
      $className = implode("/",$map_data);
      if (file_exists($className.".inc")){
        require_once $className.".inc";
      }
    });

    /*--------------PhpInit----------------*/
    PhpInit::loadClass();
    PhpInit::init();
    /*--------------PhpInit----------------*/

    $request_url = $_SERVER['REQUEST_URI'];
    $api = str_replace("/","\\",$request_url);
    $request = new \Common\Request();
    $response = new \Common\Response();

    $request->api = $request_url;
    $request->uid = "";
    $request->data = file_get_contents("php://input");

    if (class_exists($api)){
      /**
       * @var $api \Common\Api
       */
      $api = new $api;
      $api->process($request, $response);
    }else{
      $response->httpStatus = \Common\HttpStatus::NOT_FOUND;
      $response->httpMessage = "Api Is Not Found";
    }

    if ($response->httpStatus != \Common\HttpStatus::OK){
      header("HTTP/1.1 ".$response->httpStatus." ".$response->httpMessage);
      return;
    }

    foreach ($response->httpHeaders as $key => $header){
      header($key.":".$header);
    }

    if (!empty($response->data)){
      file_put_contents("php://output",$response->data);
    }

  }
}

Index::main();